import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import net.miginfocom.swing.*;
/*
 * Created by JFormDesigner on Sun Dec 08 17:23:37 GMT+08:00 2024
 */



/**
 * @author 22387
 */
public class Lgoin_user extends JFrame {
    public Lgoin_user() {
        initComponents();
    }

    private void button2MouseClicked(MouseEvent e) {
        user_register userRegister=new user_register();
        userRegister.setVisible(true);
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void button1MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        user_Home userHome=new user_Home();
        userHome.setVisible(true);
    }

    static String userid=new String();

    private void button1_check(ActionEvent e) {
            String user_ID = textField1.getText();
            String password = new String(passwordField1.getPassword());
            if (validateUser(user_ID, password)) {
                JOptionPane.showMessageDialog(this, "登录成功！");
                this.dispose();
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                user_Home userHome=new user_Home();
                userHome.setVisible(true);
            } else {
                JOptionPane.showMessageDialog(this, "登录失败，请检查用户名和密码！");
            }
            userid=user_ID;
    }
    private boolean validateUser(String user_ID, String password) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaproject",
                    "root",
                    "root");

            String query = "SELECT * FROM user_information WHERE user_ID = ? AND password = ?";
            try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
                preparedStatement.setString(1, user_ID);
                preparedStatement.setString(2, password);
                ResultSet resultSet = preparedStatement.executeQuery();

                return resultSet.next();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "数据库连接或查询出现问题：" + ex.getMessage());
            return false;
        }
    }

    private void button3MouseClicked(MouseEvent e) {
        this.dispose();
        Login_Select login_Select=new Login_Select();
        login_Select.setVisible(true);
    }






    private void initComponents() {

        //======== this ========
        setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        button3 = new JButton();
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        passwordField1 = new JPasswordField();
        button1 = new JButton();
        button2 = new JButton();

        button3.setText("\u8fd4\u56de\u4e0a\u4e00\u7ea7");
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button3MouseClicked(e);
            }
        });
        contentPane.add(button3, "cell 7 3");
        label1.setText("\u7528\u6237\u767b\u5f55");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 8f));
        contentPane.add(label1, "cell 16 3");
        label2.setText("\u7528\u6237ID\uff1a");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 3f));
        contentPane.add(label2, "cell 7 6");
        contentPane.add(textField1, "cell 10 6 17 1");
        label3.setText("\u5bc6\u7801\uff1a");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 3f));
        contentPane.add(label3, "cell 7 10");
        contentPane.add(passwordField1, "cell 10 10 17 1");
        button1.setText("\u767b\u5f55");
        button1.addActionListener(e -> button1_check(e));
        contentPane.add(button1, "cell 12 13 4 1");
        button2.setText("\u6ce8\u518c");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2, "cell 20 13");
        pack();
        setLocationRelativeTo(getOwner());


    }

    private JButton button3;
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JPasswordField passwordField1;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on



}
